package com.dinzeer.sjapadder.effect;

import com.dinzeer.sjapadder.register.SjaEffect;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

@Mod.EventBusSubscriber
public class ColderEclipse extends MobEffect {

    private static final UUID ATTACK_DAMAGE_UUID = UUID.nameUUIDFromBytes("ColderEclipse_attack_damage".getBytes());
    private static final UUID MOVEMENT_SPEED_UUID = UUID.nameUUIDFromBytes("ColderEclipse_movement_speed".getBytes());

    public ColderEclipse() {
        super(MobEffectCategory.HARMFUL, -26368);
    }

    @Override
    public void applyEffectTick(@NotNull LivingEntity entity, int amplifier) {
        if (entity.level().isClientSide) return;

        double x = 10 * (amplifier + 1); // 10% per level
        AttributeInstance attackDamage = entity.getAttribute(Attributes.ATTACK_DAMAGE);
        AttributeInstance movementSpeed = entity.getAttribute(Attributes.MOVEMENT_SPEED);

        if (attackDamage != null) {
            attackDamage.removeModifier(ATTACK_DAMAGE_UUID);
            attackDamage.addTransientModifier(new AttributeModifier(ATTACK_DAMAGE_UUID, "ColderEclipse Attack Damage", -x/100, AttributeModifier.Operation.MULTIPLY_TOTAL));
        }
        if (movementSpeed != null) {
            movementSpeed.removeModifier(MOVEMENT_SPEED_UUID);
            movementSpeed.addTransientModifier(new AttributeModifier(MOVEMENT_SPEED_UUID, "ColderEclipse Movement Speed", -x/100, AttributeModifier.Operation.MULTIPLY_TOTAL));
        }
    }


    public static float onEffectHurt(LivingEntity entity, float amount) {
        int amplifier = entity.getEffect(SjaEffect.ColdEclipse.get()).getAmplifier();
        float x = 10 * (amplifier + 1);
        return amount * (1 + x / 100);
    }

    @SubscribeEvent
    public static void doColdEclipse(LivingHurtEvent event) {
        LivingEntity entity = event.getEntity();
        if (entity.hasEffect(SjaEffect.ColdEclipse.get())){
            event.setAmount(onEffectHurt(entity, event.getAmount()));
        }
    }


    @Override
    public boolean isDurationEffectTick(int duration, int amplifier) {
        return true;
    }

    @SubscribeEvent
    public static void doColdEclipse(TickEvent.PlayerTickEvent event) {
        event.player.getAttribute(Attributes.ATTACK_DAMAGE).removeModifier(ATTACK_DAMAGE_UUID);
        event.player.getAttribute(Attributes.MOVEMENT_SPEED).removeModifier(MOVEMENT_SPEED_UUID);
    }
}